package shell.model;

import inabit.base.WorkingMemory;
import inabit.data.FireableRule;
import inabit.data.Rule;
import inabit.data.expression.ConsequentExpression;
import inabit.data.expression.Expression;
import inabit.inference.BackwardEngine;
import inabit.utils.ExpressionStringFormat;
import inabit.utils.RuleStringFormat;

import java.util.List;

import javax.swing.JOptionPane;
/**@author Rafael Rocha*/
public class MyBackwardEngine extends BackwardEngine{

	private RuleStringFormat ruleFormat;
	
	public MyBackwardEngine(RuleStringFormat ruleFormat) {
		this.ruleFormat = ruleFormat;
	}
	
	@Override
	protected FireableRule resolveSet(List<FireableRule> conflictSet) {
		return conflictSet.get(0);
	}

	@Override
	protected Double ask(Expression exp, Rule r, WorkingMemory wm) {
		int option = JOptionPane.showOptionDialog(null, ruleFormat.getExpressionFormat().format(exp) + "?", "This is a test!", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null);
		switch(option) {
		case JOptionPane.YES_OPTION:
			return 1.0;
		case JOptionPane.NO_OPTION:
			return 0.0;
		case JOptionPane.CANCEL_OPTION:
			return null;
		}
		return null;
	}

	@Override
	protected String getInnerNodeLabel(Rule r) {
		ExpressionStringFormat expFormat = ruleFormat.getExpressionFormat();
		return r.getID() + " : " + expFormat.format(r.getPrecedent());
	}

	@Override
	protected String getRootNodeLabel(String goal) {
		
		return goal + "?";
	}

	@Override
	protected String getGoalNodeLabel(Expression exp) {
		ExpressionStringFormat expFormat = ruleFormat.getExpressionFormat();
		return expFormat.format(exp);
	}

}
